#ifndef _CPU_INFO_H_
#define _CPU_INFO_H_

#include <stdint.h>
#include <stdio.h>

// #ifndef __linux__
// #error only support linux system
// #endif

#ifdef __x86_64__
#define COMPLATE_MESSAGE "Compiled for 64-bit x86_64 architecture"
#define CPU_ITEM_SIZE uint64_t
#elif defined(__i386__)
#define COMPLATE_MESSAGE "Compiled for 32-bit x86 architecture"
#define CPU_ITEM_SIZE uint32_t
#elif defined(__aarch64__)
#define COMPLATE_MESSAGE "Compiled for 64-bit ARM64 (aarch64) architecture"
#define CPU_ITEM_SIZE uint64_t
#elif defined(_WIN64)
#define COMPLATE_MESSAGE "Compiled for 64-bit Windows"
#define CPU_ITEM_SIZE uint64_t
#elif defined(__arm__) && !defined(__aarch64__)
#define COMPLATE_MESSAGE "Compiled for 32-bit ARM architecture"
#define CPU_ITEM_SIZE uint32_t
#else
#define COMPLATE_MESSAGE "Unknown architecture"
#define CPU_ITEM_SIZE uint32_t
#endif

// ======== cpu use rate ========
/* cpu使用率核心数据信息结构体定义 */
struct CpuBaseInfo {
    char name[32];           /* cpu名称 */
    CPU_ITEM_SIZE user;       /* 用户态CPU时间 */
    CPU_ITEM_SIZE nice;       /* 低优先级用户态CPU时间 */
    CPU_ITEM_SIZE system;     /* 内核态CPU时间 */
    CPU_ITEM_SIZE idle;       /* 空闲CPU时间 */
    CPU_ITEM_SIZE iowait;     /* 等待I/O操作CPU时间 */
    CPU_ITEM_SIZE irq;        /* 硬中断CPU时间 */
    CPU_ITEM_SIZE softirq;    /* 软中断CPU时间 */
    CPU_ITEM_SIZE steal;      /* 虚拟机CPU时间 */
    CPU_ITEM_SIZE guest;      /* 虚拟机低优先级CPU时间 */
    CPU_ITEM_SIZE guest_nice; /* 虚拟机低优先级用户态CPU时间 */
};

struct CpuUsageRate {
    FILE *fp;
    int cpu_num;    // cpu total core + 1
    struct CpuBaseInfo *ninfo;
    float *cpu_usage_rate;  // present(%) unit
    int cpu_temperature;    // cpu_temperature/1000 = °C
};

int CpuUsageRateInit(struct CpuUsageRate *cpu);     // 初始化
int GetCpuUsageRate(struct CpuUsageRate *cpu);      // 获取使用率
void CpuUsageRateDeinit(struct CpuUsageRate *cpu);  // 释放

#endif /* _CPU_INFO_H_ */
